package com.qf.leadnewswemedia.service.impl;

import com.qf.leadnewsmodel.dtos.ResponseResult;
import com.qf.leadnewsmodel.enums.TaskTypeEnum;
import com.qf.leadnewsmodel.pojos.wemedia.WmNews;
import com.qf.leadnewswemedia.feign.ScheduleFeignApi;
import com.qf.leadnewswemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Component
@Slf4j
public class AutoScheduleService {


    @Autowired
    private WmNewsService wmNewsService;

    @Autowired
    private ScheduleFeignApi scheduleFeignApi;

    @Scheduled(cron = "0/5 * * * * ? ")
    public void publishNews() throws UnsupportedEncodingException {
        log.info("===自动发布文章===");
        ResponseResult responseResult = scheduleFeignApi.consumeTask(TaskTypeEnum.NEWS_SCAN_TIME.getTaskType(), TaskTypeEnum.NEWS_SCAN_TIME.getPriority());

        if (responseResult != null) {
            String newsId = (String) responseResult.getData();

            WmNews wmNews = wmNewsService.getById(newsId);
            if (wmNews != null) {
                //审核--发布 (当前方法没有事务，调用的方法是有事务)
                wmNewsService.autoScan(wmNews);
            }
        }
    }

}
